package cn.woochen.comlib.util

import android.content.Context
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader

object FileUtils {

    /**
     * 加载assets中的文件,并转化为字符串
     */
     fun assetFile2Str(c: Context, urlStr: String?): String? {
        var `in`: InputStream? = null
        try {
            `in` = c.assets.open(urlStr!!)
            val bufferedReader = BufferedReader(InputStreamReader(`in`))
            var line: String? = null
            val sb = StringBuilder()
            do {
                line = bufferedReader.readLine()
                if (line != null && !line.matches(Regex("^\\s*\\/\\/.*"))) {
                    sb.append(line)
                }
            } while (line != null)
            bufferedReader.close()
            `in`.close()
            return sb.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (`in` != null) {
                try {
                    `in`.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return null
    }
}